<%@ page import="vo.Teacher" %>
<%@ page import="vo.Course" %>
<%@ page import="dao.CourseDao" %>
<%@ page import="dao.AttendanceDao" %>
<%@ page import="java.util.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="../resources/css/jquery-ui-1.10.4.custom.min.css">
    <script src="../resources/js/jquery-1.10.2.js"></script>
    <script src="../resources/js/jquery-ui-1.10.4.custom.min.js"></script>
    <title>main</title>
    <link href="../resources/css/default.css" rel="stylesheet"/>
</head>
<body>
<%
    Teacher teacher = (Teacher) session.getAttribute("info");
    int sumIndex = (int) session.getAttribute("sumIndex");
%>
<div id="page" class="container">
    <div id="header">
        <div id="logo">
            <img src="../userImg/<%=teacher.getId()%>.jpeg"/>
            <h1><%=teacher.getId()%></h1>
        </div>
        <div id="menu">
            <ul>
                <li><a href="personal.jsp">个人信息</a></li>
                <li><a href="../one_page_student">学生管理</a></li>
                <li><a href="../one_page_course">课程管理</a></li>
                <li class="current_page_item"><a href="../one_page_attendance?teacher_id=<%=teacher.getId()%>">出勤管理</a></li>
                <li><a href="../one_page_score">成绩管理</a></li>
                <li><a href="../one_page_tuition_records">学费管理</a></li>
                <li><a onclick="return confirm('确认退出?');" href="../exit">退出登录</a></li>
            </ul>
        </div>
    </div>
    <div id="main">
        <div class="top">
            <h2>学生出勤管理</h2>
            <hr/>
            <button class="btn-add">添加考勤记录</button>
        </div>
            <div class="table" style="margin-top: 20px; height: 525px">
                <table id="table" width="800" frame="box" align="center">
                    <tr>
                        <th height="35">课程</th>
                        <th>学生人数</th>
                        <th>缺勤人数</th>
                        <th>到课人数</th>
                        <th>请假人数</th>
                        <th style="width: auto">时间</th>
                        <th style="margin-left: 100px">操作</th>
                    </tr>
                    <%
                        try {
                            AttendanceDao attendanceDao = new AttendanceDao();
                            Map<String, String> courses;
                            if(session.getAttribute("attendanceMap") != null){
                                courses = (Map<String,String>)session.getAttribute("attendanceMap");
                            }else {
                                courses = attendanceDao.getCoursesByTeacher(teacher.getId());
                            }
                            for (Map.Entry<String, String> entry : courses.entrySet()) {
                                int sum = attendanceDao.getSumByCourseIdAndTime(entry.getValue(),entry.getKey());
                                int absence = attendanceDao.getAbsenceByCourseIdAndTime(entry.getValue(),entry.getKey());
                                int leave = attendanceDao.getLeaveByCourseIdAndTime(entry.getValue(),entry.getKey());
                                int arrive = sum - absence-leave;
                                CourseDao courseDao = new CourseDao();
                                Course course = courseDao.getCourseById(entry.getValue());
                    %>
                    <tr>
                        <td height="35"><%=course.getName()%></td>
                        <td><%=sum%></td>
                        <td><%=absence%></td>
                        <td><%=arrive%></td></td>
                        <td><%=leave%></td></td>
                        <td><%=entry.getKey()%></td>
                        <td><a class="btn-detail"
                               href="../one_page_attendance_detail?index=1&attendance_time=<%=entry.getKey()%>&teacherId=<%=teacher.getId()%>">查看考勤</a></td>
                        <td><a class="btn-delete"
                               onclick="return confirm('确定要删除吗?');"
                               href=<%="'../delete_attendance?teacher_id=" + teacher.getId() + "&attendance_time=" + entry.getKey() +"'"%>>删除</a></td>
                        </td>
                    </tr>
                    <%
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    %>
                </table>
            </div>

        <%
            if (sumIndex > 1){
        %>
        <div id="index">
            <a href="../one_page_attendance?index=1&teacher_id=<%=teacher.getId()%>">首页</a>
            <%
                for (int i = 1; i <= sumIndex; i++) {
            %>
            <a href="../one_page_attendance?index=<%=i%>&teacher_id=<%=teacher.getId()%>">第<%=i%>页</a>
            <%
                }
            %>
            <a href="../one_page_attendance?index=<%=sumIndex%>&teacher_id=<%=teacher.getId()%>">尾页</a>
        </div>
        <%
            }
        %>

    </div>
</div>



<div id="add-dialog" title="添加考勤信息">
    <form id="add-form" method="post">
        课程名称:<select name="course_id">
            <%
                try {
                    CourseDao courseDao = new CourseDao();
                    Set<Course> courses = courseDao.getCoursesByTeacherId(teacher.getId());
                    for (Course course : courses) {
            %>
            <option value="<%=course.getId()%>"><%=course.getName()%></option>
            <%
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            %>
        </select><br/>
        时间：<input type="datetime-local" name="attendance_time"/><br/>
        <input type="hidden" name="teacher_id" value="<%=teacher.getId()%>">
        <hr>
        <input style="float: right" type="submit" value="取消" onclick="function x(){
               $('#add-dialog').dialog('close');
        }">
        <input style="float: right; margin-right: 25px " type="submit" value="确定"
        onclick="this.form.action='../add_attendance'">
    </form>
</div>

<style>
    .ui-dialog-titlebar-close {
        display: none
    }
</style>

<script>
    $('#add-dialog').dialog({
        width:310,
        autoOpen:false,
        draggable:false,
        modal:true,
        resizeable:false
    });
    $('.btn-add').click(function () {
        $('#add-dialog').dialog('open');
    });
</script>
</body>
</html>